<script lang="ts" setup name="GoodsImage">
import { useMouseInElement } from '@vueuse/core';
import { computed, ref } from 'vue';

defineProps<{
  images: string[]
}>()

// 高亮的变量
const active = ref(0)

const target = ref(null)
const { elementX, elementY, isOutside } = useMouseInElement(target)

const position = computed(() => {
  // 让遮罩层跟着鼠标移动
  // 让鼠标在盒子中心
  const position = {
    x: elementX.value - 100,
    y: elementY.value - 100
  }

  // 边界处理
  if (position.x < 0) position.x = 0
  if (position.y < 0) position.y = 0
  if (position.x > 200) position.x = 200
  if (position.y > 200) position.y = 200

  return position
})

// console.log(x, y, elementX, elementY)

</script>

<template>
  <!-- <p>这里是响应式数据x: {{ x }}, y: {{ y }}, elementX: {{ elementX }}, elementY: {{ elementY }}</p> -->
  <div class="goods-image">
    <!-- 显示在右侧的放大之后的区域 -->
    <div class="large"
         v-show="!isOutside"
    :style="[{backgroundImage:`url(${ images[active] })`, backgroundPosition: `-${ position.x * 2 }px -${ position.y * 2 }px`}]"></div>

    <div class="middle" ref="target">
      <img :src="images[active]" alt="" />
      <!-- 移动遮罩 -->
      <div class="layer" v-show="!isOutside" :style="{ left: position.x + 'px', top: position.y + 'px' }"></div>
    </div>
    <ul class="small">
      <li 
        @mouseenter="active = index"
        v-for="(item, index) in images" 
        :key="item" 
        :class="{ active: index === active }">
        <img
          :src="item"
          alt=""
        />
      </li>
    </ul>
  </div>
</template>

<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    background-repeat: no-repeat;
    // 放大一倍
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0,0,0,.2);
      left: 0;
      top: 0;
      // 可以移动
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>